<template>
	<view>
		<view class="login-wrap" v-show="showLogin">
			<h3>登录</h3>
			<p v-show="showTishi">{{tishi}}</p>
			<input type="text" placeholder="请输入用户名" v-model="username">
			<input type="password" placeholder="请输入密码" v-model="password">
			<button v-on:click="login">登录</button>
			<span v-on:click="ToRegister">没有账号？马上注册</span>
		</view>

		<view class="register-wrap" v-show="showRegister">
			<h3>注册</h3>
			<p v-show="showTishi">{{tishi}}</p>
			<input type="text" placeholder="*请输入用户名" v-model="newUsername">
			<input type="password" placeholder="*请输入密码" v-model="newPassword">
			<input type="text" placeholder="请输入昵称" v-model="newNickname">
			<view class="uni-list-cell-db">
				<picker @change="bindPickerChange" :value="index" :range="mpNameArray">
					<view class="uni-input">{{mpNameArray[index]}}</view>
				</picker>
			</view>
			<button v-on:click="register">注册</button>
			<span v-on:click="ToLogin">已有账号？马上登录</span>
		</view>
	</view>
</template>

<style>
	.login-wrap {
		text-align: center;
	}

	.register-wrap {
		text-align: center;
	}

	.reg-list {
		display: block;
		width: 250px;
		height: 40px;
		line-height: 40px;
		margin: 0 auto;
		margin-bottom: 10px;
		outline: none;
		border: 1px solid #888;
		padding: 10px;
		box-sizing: border-box;
	}

	input {
		display: block;
		width: 250px;
		height: 40px;
		line-height: 40px;
		margin: 0 auto;
		margin-bottom: 10px;
		outline: none;
		border: 1px solid #888;
		padding: 10px;
		box-sizing: border-box;
	}

	p {
		color: red;
	}

	button {
		display: block;
		width: 250px;
		height: 40px;
		line-height: 40px;
		margin: 0 auto;
		border: none;
		background-color: #41b883;
		color: #fff;
		font-size: 16px;
		margin-bottom: 5px;
	}

	span {
		cursor: pointer;
	}

	span:hover {
		color: #41b883;
	}
</style>

<script>
	// import {
	// 	setCookie,
	// 	getCookie
	// } from '../../static/js/cookie.js'
	export default {
		data() {
			return {
				username: '',
				password: '',
				newUsername: '',
				newPassword: '',
				newNickname: '',
				mgxHashID: '',
				tishi: '',
				showTishi: false,
				showLogin: true,
				showRegister: false,
				mpNameArray: ['选择矿池', 'A矿池', 'B矿池', 'C矿池', 'D矿池'],
				index: 0,
				mpValueArray: ['http://106.75.168.107:8001',
					'http://106.75.168.107:8003',
					'http://106.75.132.200:8002',
					'http://106.75.132.200:8004'
				]
			}
		},
		// mounted() {
		// 	if (getCookie('username')) {
		// 		this.$router.push('/home')
		// 	}
		// },
		methods: {
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.mgxHashID = this.mpValueArray[e.target.value - 1]
				console.log(this.mgxHashID)
			},
			login() {
				if (this.username == "" || this.password == "") {
					uni.showToast({
						title: '用户名及密码不能为空',
						icon: "none",
						duration: 2000
					});
				} else {
					uni.request({
						url: 'https://www.jsonfree.com/mgxapi/api/Auth/Login',
						method: 'POST',
						data: {
							'Source': 2,
							'LoginName': this.username,
							'PassWord': this.password,
							'IsData': 1
						},
						success: res => {
							console.log(res.data)
							console.log(res.data.Code)
							if (res.data.Code == -1) {
								uni.showToast({
									title: '登录失败：' + res.data.Msg,
									icon: "none",
									duration: 5000
								});
							} else if (res.data.Code == 0) {
								uni.showToast({
									title: '登录成功',
									icon: "success",
									duration: 2000
								});
								this.tishi = "登录成功"
								this.showTishi = true
								// setCookie('username', this.username, 1000 * 60)

								getApp().globalData.SessionID = res.data.SessionID
								getApp().globalData.userData.UserID = res.data.Data['UserID']
								getApp().globalData.userData.LoginName = res.data.Data['LoginName']
								getApp().globalData.userData.UserName = res.data.Data['UserName']
								getApp().globalData.userData.MgxHashID = res.data.Data['MgxHashID']
								getApp().globalData.userData.ERCAddress = res.data.Data['ERCAddress']
								getApp().globalData.userData.Email = res.data.Data['Email']
								getApp().globalData.userData.AvatarUrl = res.data.Data['AvatarUrl']
								uni.switchTab({
									url: '/pages/mangox/index'
								})
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
			ToRegister() {
				this.showRegister = true
				this.showLogin = false
			},
			ToLogin() {
				this.showRegister = false
				this.showLogin = true
			},
			register() {
				if (this.newUsername == "" || this.newPassword == "") {
					uni.showToast({
						title: '用户名及密码不能为空',
						icon: "none",
						duration: 2000
					});
				} else {
					uni.request({
						url: 'https://www.jsonfree.com/mgxapi/api/UserMgx/CreateUser',
						method: 'POST',
						data: {
							'Source': 2,
							'LoginName': this.newUsername,
							'Password': this.newPassword,
							'UserName': this.newNickname
						},
						success: res => {
							console.log(res.data)
							console.log(res.data.Code)
							if (res.data.Code == -1) {
								uni.showToast({
									title: '注册失败：' + res.data.Msg,
									icon: "none",
									duration: 5000
								});
							} else if (res.data.Code == 0) {
								uni.showToast({
									title: '注册成功,请登录',
									icon: "success",
									duration: 5000
								});
								this.tishi = "注册成功"
								this.showTishi = true
								this.username = ''
								this.password = ''
								setTimeout(function() {
									this.showRegister = false
									this.showLogin = true
									this.showTishi = false
								}.bind(this), 1000)
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			}
		}
	}
</script>
